
//
//  SportSpeedView.m
//  RunningMan
//
//  Created by dllo on 16/5/26.
//  Copyright © 2016年 WSJ. All rights reserved.
//

#import "SportSpeedView.h"

@interface SportSpeedView ()

@end

@implementation SportSpeedView


-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {

        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width / 2 - 100, self.frame.size.height - 30, 200, 30)];
        label.text = @"速度图表";
        label.textAlignment = 1;
        label.textColor = [UIColor blueColor];
        [self addSubview:label];
    }
    return self;
}

- (void)drawRect:(CGRect)rect{
    NSLog(@"%s", __FUNCTION__);
    
    CGFloat max = 0;
    CGFloat min = MAXFLOAT;
    NSInteger  count = self.speedArr.count;
    for (NSString *speed in self.speedArr) {
       CGFloat num = ABS(speed.floatValue);
       // CGFloat num = speed.floatValue;
        
        max = max > num ? max : num;
        min = min < num ? min : num;
    }
    
    CGFloat hei = rect.size.height ;
    CGFloat wid = rect.size.width;
    if (max != min) {
        
        
        CGFloat X =  (wid - 0.2 * hei) / count * 1.0;
        CGFloat H = hei * 0.8 / max;
        NSLog(@"=======X=%f, y = %f", X, H);
        
        // 获得绘图上下文
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        // curve  创建路径
        CGMutablePathRef pathRoute = CGPathCreateMutable();
        // 指定坐标(路径起点)
         CGPathMoveToPoint(pathRoute, nil, 0.1 * hei + X, 0.9 * hei - ABS([[self.speedArr firstObject]   floatValue ]) * H);
       
        for (int i = 1; i < self.speedArr.count; i++) {
            NSString *speed = self.speedArr[i];
            // 指定路径至指定的坐标
            
            CGPathAddLineToPoint(pathRoute, nil, 0.1 * hei + (i + 1) * X, 0.9 * hei - ABS(speed.floatValue) * H);
            CGPathMoveToPoint(pathRoute, nil, 0.1 * hei + (i + 1) * X, 0.9 * hei - ABS(speed.floatValue) * H);
            
            // 添加路径到上下文
            CGContextAddPath(context, pathRoute);
       
        }
        
       // CGPathAddLineToPoint(pathRoute, nil, wid - 0.1 * hei, 0.9 * hei);
       CGPathAddLineToPoint(pathRoute, nil, 0.1 * hei + (self.speedArr.count) * X,0.9 * hei - ABS([[self.speedArr lastObject]   floatValue ]) * H);
        
        CGContextAddPath(context, pathRoute);
        
        CGContextSetRGBStrokeColor(context, 1.0, 0., 0, 1.0);
        // 设置线的宽度
        CGContextSetLineWidth(context, 2.0);
        //设置结合点, 点的类型
        CGContextSetLineJoin(context, kCGLineJoinRound);
        CGContextDrawPath(context, kCGPathStroke);
        
       [self drawLinearGradient:context path:pathRoute startColor:[UIColor colorWithRed:248 / 255.0 green:248 / 255.0 blue:248 / 255.0 alpha:1.0].CGColor endColor:[UIColor colorWithRed:91 / 255.0 green:199 / 255.0 blue:150 / 255.0 alpha:1.0].CGColor];
        // 释放
     CGPathRelease(pathRoute);
        
        
        CGMutablePathRef path = CGPathCreateMutable();
   
            CGPathMoveToPoint(path, nil, 0.1 * hei, 0.1 * hei );
            CGPathAddLineToPoint(path, nil,  0.1 * hei, 0.9 * hei);
        
           CGPathMoveToPoint(path, nil, 0.1 * hei,  0.9* hei);
           CGPathAddLineToPoint(path, nil,   wid -  0.1 * hei,  0.9 * hei);
        
            CGContextAddPath(context, path);
        
            CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1);
            CGContextSetLineWidth(context, 2.0);
            CGContextDrawPath(context, kCGPathFillStroke);
            CGPathRelease(path);
        
        
        
        // baselines
//        CGMutablePathRef path = CGPathCreateMutable();
//        for (int i = 0; i < 3; i++) {
//            CGPathMoveToPoint(path, nil, 0.1 * hei, 0.1 * hei + 0.4 * i * hei);
//            CGPathAddLineToPoint(path, nil, wid - 0.1 * hei, 0.1 * hei + 0.4 * i * hei);
//            CGContextAddPath(context, path);
   //   }
        
//        CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1);
//        CGContextSetLineWidth(context, 2.0);
//        CGContextDrawPath(context, kCGPathFillStroke);
//        CGPathRelease(path);
        
        
    }
}


- (void)drawLinearGradient:(CGContextRef)context
                      path:(CGPathRef)path
                startColor:(CGColorRef)startColor
                  endColor:(CGColorRef)endColor
{
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGFloat locations[] = { 0.0, 1.0 };
    NSArray *colors = @[(__bridge id) startColor, (__bridge id) endColor];
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);
    CGRect pathRect = CGPathGetBoundingBox(path);
    //具体方向可根据需求修改
    CGPoint startPoint = CGPointMake(CGRectGetMidX(pathRect), CGRectGetMinY(pathRect));
    CGPoint endPoint = CGPointMake(CGRectGetMidX(pathRect), CGRectGetMaxY(pathRect));
    CGContextSaveGState(context);
    CGContextAddPath(context, path);
    CGContextClip(context);
    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
    CGContextRestoreGState(context);
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorSpace);
}




@end
